package com.xbongbong.saas.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.service.CustomerRuleService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseVO;
import com.xbongbong.pro.dictionary.pojo.DictionaryPojo;
import com.xbongbong.pro.dictionary.pojo.dto.DictionaryListDTO;
import com.xbongbong.pro.dictionary.pojo.dto.FormDataDictionaryListDTO;
import com.xbongbong.pro.dictionary.pojo.vo.DictionaryVO;
import com.xbongbong.pro.enums.ErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.DataDictionaryErrorCodeEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.fromdatadictionary.pojo.dto.FormDataDictionaryAddDTO;
import com.xbongbong.saas.domain.entity.DataDictionaryEntity;
import com.xbongbong.saas.domain.entity.FormDataDictionaryEntity;
import com.xbongbong.saas.enums.dictionary.DictionaryEnum;
import com.xbongbong.saas.enums.dictionary.DictionarySpecialEnum;
import com.xbongbong.saas.enums.dictionary.DictionaryTermEnum;
import com.xbongbong.saas.model.DataDictionaryModel;
import com.xbongbong.saas.model.FormDataDictionaryModel;
import com.xbongbong.saas.service.FormDataDictionaryService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/1/31 9:03
 * @since v1.0
 */
@Service("formDataDictionaryService")
public class FormDataDictionaryServiceImpl implements FormDataDictionaryService {
    @Resource
    private FormDataDictionaryModel formDataDictionaryModel;
    @Resource
    private DataDictionaryModel dataDictionaryModel;
    @Resource
    private PaasFormModel paasFormModel;
    @Resource
    private CustomerRuleService customerRuleService;

    @Override
    public BaseVO add(FormDataDictionaryAddDTO formDataDictionaryAddDTO) throws XbbException {
        List<DictionaryPojo> list =  formDataDictionaryAddDTO.getList();

        DataDictionaryEntity dbDataDictionary = dataDictionaryModel.getByKey(formDataDictionaryAddDTO.getId(),formDataDictionaryAddDTO.getCorpid());
        FormDataDictionaryEntity dbFormDataDictionary = formDataDictionaryModel.getByKey(formDataDictionaryAddDTO.getId(),formDataDictionaryAddDTO.getCorpid());

        Integer type = formDataDictionaryAddDTO.getType();
        DictionaryEnum dictionaryEnum = DictionaryEnum.getEnumByCode(type);
        DictionaryTermEnum dictionaryTermEnum = dictionaryEnum.getDictionaryTermEnum();
        List<DictionaryPojo> dbList = new ArrayList<>();
        if (dictionaryTermEnum == DictionaryTermEnum.DATA_DICTIONARY_TERM) {
            dbList = JSONArray.parseArray(dbDataDictionary.getData(),DictionaryPojo.class);
        } else if (dictionaryTermEnum == DictionaryTermEnum.FORM_DATA_DICTIONARY_TERM) {
            dbList = JSONArray.parseArray(dbFormDataDictionary.getData(),DictionaryPojo.class);
        }


        Integer maxCode = 0;
        for (DictionaryPojo pojo : dbList){
            // 物流公司不是数字类型的数据字典
            try {
                Integer code = Integer.valueOf(pojo.getValue());
                maxCode = code > maxCode ? code : maxCode;
            } catch (Exception e) {

            }
        }
        Set<String> nameSet = new HashSet<>();
        Set<String> codeSet = new HashSet<>();
        for (DictionaryPojo pojo : list){
            if (pojo.getValue() == null){
                pojo.setValue((++maxCode).toString());
            }
            nameSet.add(pojo.getText());
            codeSet.add(pojo.getValue());
        }
        if (nameSet.size() < list.size()){
            throw new XbbException(DataDictionaryErrorCodeEnum.API_ERROR_370002);
        }
        if (codeSet.size() < list.size()){
            throw new XbbException(DataDictionaryErrorCodeEnum.API_ERROR_370001);
        }

        if (dictionaryEnum.getDictionaryTermEnum() == DictionaryTermEnum.DATA_DICTIONARY_TERM) {
            dataDictionaryModel.update(formDataDictionaryAddDTO.getId(), formDataDictionaryAddDTO.getCorpid(),JSONObject.toJSONString(list));
        } else {
            formDataDictionaryModel.update(formDataDictionaryAddDTO.getId(), formDataDictionaryAddDTO.getCorpid(),JSONObject.toJSONString(list), dbFormDataDictionary.getFormId());
        }

        Long formId = formDataDictionaryAddDTO.getFormId();
        if (dictionaryEnum == DictionaryEnum.CUSTOMER_STATUS || dictionaryEnum == DictionaryEnum.CONTRACT_STATUS) {
            formId = null;
        }

        formDataDictionaryModel.removeRedis(formDataDictionaryAddDTO.getCorpid(), formId, dictionaryEnum);
        if (dictionaryEnum == DictionaryEnum.CUSTOMER_STATUS || dictionaryEnum == DictionaryEnum.CONTRACT_STATUS) {
            String corpid = formDataDictionaryAddDTO.getCorpid();
            customerRuleService.customerTypeChage(corpid);
        }
        return new BaseVO();
    }

    @Override
    public DictionaryVO list(FormDataDictionaryListDTO formDataDictionaryListDTO) throws XbbException {
        Long formId = formDataDictionaryListDTO.getFormId();
        String corpid = formDataDictionaryListDTO.getCorpid();
        PaasFormEntityExt form = paasFormModel.getByKey(formId, corpid);

        if (form == null) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
        }
        Integer businessType = form.getBusinessType();
        String saasAttr = formDataDictionaryListDTO.getSaasAttr();

        DictionarySpecialEnum dictionarySpecialEnum =  DictionarySpecialEnum.getEnum(businessType, saasAttr);

        if (dictionarySpecialEnum == DictionarySpecialEnum.UNKNOWN) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100018);
        }
        DictionaryListDTO dictionaryListDTO = new DictionaryListDTO();
        dictionaryListDTO.setFormId(formId);
        dictionaryListDTO.setType(dictionarySpecialEnum.getDictionaryEnum().getType());
        dictionaryListDTO.setCorpid(corpid);
        return formDataDictionaryModel.list(dictionaryListDTO);
    }
}
